﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class AdminFlag
    {
        private readonly KeRui.DAL.AdminFlag dal = new KeRui.DAL.AdminFlag();

        public int Add(KeRui.Model.AdminFlag model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.AdminFlag> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.AdminFlag> list = new List<KeRui.Model.AdminFlag>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.AdminFlag item = new KeRui.Model.AdminFlag();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["ParentID"].ToString() != "")
                    {
                        item.ParentID = int.Parse(dt.Rows[i]["ParentID"].ToString());
                    }
                    item.Flag = dt.Rows[i]["Flag"].ToString();
                    item.FlagName = dt.Rows[i]["FlagName"].ToString();
                    item.FlagAction = dt.Rows[i]["FlagAction"].ToString();
                    if (dt.Rows[i]["FlagType"].ToString() != "")
                    {
                        item.FlagType = int.Parse(dt.Rows[i]["FlagType"].ToString());
                    }
                    if (dt.Rows[i]["IsNav"].ToString() != "")
                    {
                        item.IsNav = int.Parse(dt.Rows[i]["IsNav"].ToString());
                    }
                    item.AppUrl = dt.Rows[i]["AppUrl"].ToString();
                    item.Icon = dt.Rows[i]["Icon"].ToString();
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["IsOpen"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsOpen"].ToString() == "1") || (dt.Rows[i]["IsOpen"].ToString().ToLower() == "true"))
                        {
                            item.IsOpen = true;
                        }
                        else
                        {
                            item.IsOpen = false;
                        }
                    }
                    if (dt.Rows[i]["HaveChildNav"].ToString() != "")
                    {
                        if ((dt.Rows[i]["HaveChildNav"].ToString() == "1") || (dt.Rows[i]["HaveChildNav"].ToString().ToLower() == "true"))
                        {
                            item.HaveChildNav = true;
                        }
                        else
                        {
                            item.HaveChildNav = false;
                        }
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int ID)
        {
            return this.dal.Delete(ID);
        }

        public DataSet GetCheckList(int FlagType)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("AdminFlag", "  FlagType = " + FlagType + "", "OrderID asc", " ID,FlagName");
        }

        public List<KeRui.Model.AdminFlag> GetList(string strWhere)
        {
            DataSet set = new KeRui.DAL.Common().GetList("AdminFlag", -1, -1, strWhere, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.AdminFlag> GetList(int parentID, int flagType)
        {
            string strWhere = "ParentID = " + parentID;
            if (flagType != -1)
            {
                strWhere = strWhere + "AND FlagType=" + flagType;
            }
            DataSet set = new KeRui.DAL.Common().GetList("AdminFlag", -1, -1, strWhere, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.AdminFlag> GetList(int ParentID, int FlagType, int IsNav, string FlagAction)
        {
            string strWhere = "ParentID = " + ParentID;
            if (FlagType != -1)
            {
                strWhere = strWhere + " and FlagType=" + FlagType.ToString();
            }
            if ((IsNav == 0) || (IsNav == 1))
            {
                strWhere = strWhere + " and IsNav=" + IsNav;
            }
            FlagAction = FlagAction.ToLower();
            if (FlagAction != "")
            {
                strWhere = strWhere + " and FlagAction='" + Utils.SqlStringFormat(FlagAction) + "'";
            }
            DataSet set = new KeRui.DAL.Common().GetList("AdminFlag", -1, -1, strWhere, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public int GetMaxOrderID()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetMaxOrderID(" AdminFlag ", " OrderID ");
        }

        public KeRui.Model.AdminFlag GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.AdminFlag GetModelByCache(int ID)
        {
            string cacheKey = "AdminFlagModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.AdminFlag) cache;
        }

        public DataSet GetRoleChildFlag(int ParentID, int FlagType)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("AdminFlag ", string.Concat(new object[] { " ParentID =", ParentID, " AND FlagType=", FlagType, "" }), " ID ", " ID,FlagName,HaveChildNav,Icon,Flag");
        }

        public int Update(KeRui.Model.AdminFlag model)
        {
            return this.dal.Update(model);
        }
    }
}

